﻿using System;
using System.Collections.Specialized;
using System.Data.Services.Client;
using System.IO;
using AzureCamp.RssAggregatorBusiness;
using Microsoft.Samples.ServiceHosting.StorageClient;
using Microsoft.ServiceHosting.ServiceRuntime;
using AzureCamp.Table.RssAggregatorBusiness;

namespace RSSAggregator_WebRole
{
    public partial class _Default : System.Web.UI.Page
    {
        private StorageAccountInfo accountInfo = StorageAccountInfo.GetAccountInfoFromConfiguration("TableStorageEndpoint");


        private void SampleFeeds()
        {
            Feed rss = new Feed("cloudcomputing", "techno");
            rss.Owner = "Ronny";
            rss.AddRssLink("http://blogs.msdn.com/jnak/rss.xml");
            rss.AddRssLink("http://blog.smarx.com/atompub.svc/blog/posts/?fmt=rss");
            rss.AddRssLink("http://www.zecloud.fr/syndication.axd");
            rss.AddRssLink("http://www.cloudmagazine.fr/dotclear/index.php?feed/rss2");


            Feed rss2 = new Feed("stevesfartz", "techno");
            rss2.Owner = "Ronny";
            rss2.AddRssLink("http://blogs.msdn.com/ssfartz/rss.xml");
            rss2.AddRssLink("http://blogs.msdn.com/cloudcomputing/rss.xml");
            rss2.AddRssLink("http://blog.sfartz.com/feed/atom");
            rss2.AddRssLink("http://blogs.msdn.com/silverlight_plus_java/rss.xml");

            RSSAggregatorContext ctx = new RSSAggregatorContext(this.accountInfo);
            //RssLink l = new RssLink("cloudcomputing", "http://www.zecloud.fr/syndication.axd");
            ctx.AddFeed(rss);
            ctx.AddFeed(rss2);
            //ctx.RemoveRss("http://blog.sfartz.com/feed/atom");

            //ctx.AddLink(l);
            ctx.SaveChanges(SaveChangesOptions.ContinueOnError);
        }

        private static BlobContainer GetContainer(string containerName)
        {
            BlobStorage blobStorage = BlobStorage.Create(StorageAccountInfo.GetDefaultBlobStorageAccountFromConfiguration());
            //BlobContainer newContainer = blobStorage.GetBlobContainer(RoleManager.GetConfigurationSetting("ContainerName"));
            BlobContainer newContainer = blobStorage.GetBlobContainer(containerName);
            newContainer.CreateContainer(null, ContainerAccessControl.Public);
            return newContainer;
        }

        public static void SaveRssInBlob(Feed rss)
        {
            NameValueCollection metadata = new NameValueCollection();
            metadata["Id"] = rss.Title;
            metadata["Filename"] = String.Format("{0}-{1}.xml", rss.Title, DateTime.Now.ToShortDateString().Replace('/', '-'));
            metadata["Date"] = DateTime.Now.ToString();

            BlobProperties properties = new BlobProperties(rss.Title);
            properties.ContentType = "text/xml";
            properties.Metadata = metadata;

            ILocalResource myIO = RoleManager.GetLocalResource("rssrepository");
            string pathToFile = Path.Combine(myIO.RootPath, metadata["Filename"]);
            Stream s = rss.GetAgregatorAsStream(pathToFile, 20);

            BlobContents contents = new BlobContents(s);
            GetContainer(rss.Category).CreateBlob(properties, contents, true);
            s.Close();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void ValidationButton_Click(object sender, EventArgs e)
        {
            string owner = TbName.Text;
            string title = TbTitle.Text;
            string category = TbCategory.Text;
            Feed feed = new Feed(title,category);
            feed.Owner = owner;

            if (!String.IsNullOrEmpty(Feed1.Text)) feed.AddRssLink(Feed1.Text);
            if (!String.IsNullOrEmpty(Feed2.Text)) feed.AddRssLink(Feed2.Text);
            if (!String.IsNullOrEmpty(Feed3.Text)) feed.AddRssLink(Feed3.Text);
            if (!String.IsNullOrEmpty(Feed4.Text)) feed.AddRssLink(Feed4.Text);
            if (!String.IsNullOrEmpty(Feed5.Text)) feed.AddRssLink(Feed5.Text);

            RSSAggregatorContext ctx = new RSSAggregatorContext(this.accountInfo);
            ctx.MergeOption = System.Data.Services.Client.MergeOption.OverwriteChanges;
            ctx.AddFeed(feed);
            ctx.SaveChanges(SaveChangesOptions.ContinueOnError);
            SaveRssInBlob(feed);
            FeedLink.NavigateUrl = String.Format("{0}/{1}/{2}",RoleManager.GetConfigurationSetting("BlobStorageEndpoint"), feed.Category, feed.Title);
            FeedLink.Text = "Feed will be soon available here!";
            ctx = null;
        }
    }
}
